import { Body, Controller, Get, Put, Query, Res } from '@nestjs/common';
import { Response } from 'express';
import { ApiResult } from '../../common/api-result';
import { PluginNacosDto, PluginNacosServiceDto, PluginNacosServiceInstanceDto } from './plugin-nacos.dto';
import { PluginEnvSetting } from '../../plugin/plugin-env-setting.entity';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { PluginNacosConfig } from './plugin-nacos-config';
import { PluginNacosService } from './plugin-nacos.service';
import { PluginNacos } from '../../plugin/plugin.entity';
import { PluginRegisterCenterConfig } from '../PluginRegisterCenterConfig';

@Controller()
export class PluginNacosController {

  constructor(private pluginNacosService: PluginNacosService,
              @InjectRepository(PluginEnvSetting)
              private pluginEnvSettingRepository: Repository<PluginEnvSetting>) {
  }

  /**
   * Nacos 服务 列表
    * @param dto
   * @param res
   */
  @Get('/api/plugin/nacos/service/list')
  async list(@Query() dto: PluginNacosDto, @Res() res: Response) {
    const pluginEnvSetting = await this.pluginEnvSettingRepository.findOne({
      pluginName: PluginNacos.name,
      envId: dto.envId,
    });
    const pluginNacosConfig: PluginRegisterCenterConfig = JSON.parse(pluginEnvSetting.config);
    const ar = new ApiResult();
    const pluginNacosServiceList: PluginNacosServiceDto[] = [];
    const serviceList: { doms: string[], count: number } = await this.pluginNacosService.serviceList(dto, pluginNacosConfig);
    for (const serviceName of serviceList.doms) {
      const pluginNacosService: PluginNacosServiceDto = {};
      pluginNacosService.name = serviceName;
      pluginNacosService.list = await this.pluginNacosService.instanceList({ serviceName: serviceName } as PluginNacosServiceInstanceDto, pluginNacosConfig);
      pluginNacosServiceList.push(pluginNacosService);
    }
    ar.result = pluginNacosServiceList;
    return res.json(ar);
  }

  /**
   * Nacos 实例 修改
    * @param dto
   * @param res
   */
  @Put('/api/plugin/nacos/instance/changeStatus')
  async status(@Body() dto: PluginNacosServiceInstanceDto, @Res() res: Response) {

    console.log('dto', dto);
    const pluginEnvSetting = await this.pluginEnvSettingRepository.findOne({
      pluginName: PluginNacos.name,
      envId: dto.envId,
    });

    console.log('pluginEnvSetting', pluginEnvSetting);
    const pluginNacosConfig: PluginNacosConfig = JSON.parse(pluginEnvSetting.config);
    const ar = new ApiResult();
    ar.result = await this.pluginNacosService.instanceChangeStatus(dto, pluginNacosConfig);
    return res.json(ar);
  }

  /**
   * 实例 详情
   * @param dto
   * @param res
   */
  @Get('/api/plugin/nacos/instance/info')
  async instanceInfo(@Query() dto: PluginNacosServiceInstanceDto, @Res() res: Response) {
    console.log('dto', dto);
    const pluginEnvSetting = await this.pluginEnvSettingRepository.findOne({
      pluginName: PluginNacos.name,
      envId: dto.envId,
    });
    console.log('pluginEnvSetting', pluginEnvSetting);
    const pluginNacosConfig: PluginNacosConfig = JSON.parse(pluginEnvSetting.config);
    const ar = new ApiResult();
    ar.result = await this.pluginNacosService.instanceInfo(dto, pluginNacosConfig);
    return res.json(ar);
  }


  /**
   * 实例 列表
   * @param dto
   * @param res
   */
  @Get('/api/plugin/nacos/instance/list')
  async instanceList(@Query() dto: PluginNacosServiceInstanceDto, @Res() res: Response) {
    console.log('dto', dto);
    const pluginEnvSetting = await this.pluginEnvSettingRepository.findOne({
      pluginName: PluginNacos.name,
      envId: dto.envId,
    });
    console.log('pluginEnvSetting', pluginEnvSetting);
    const pluginNacosConfig: PluginNacosConfig = JSON.parse(pluginEnvSetting.config);
    const ar = new ApiResult();
    ar.result = await this.pluginNacosService.instanceList(dto, pluginNacosConfig);
    return res.json(ar);
  }


}